package org.helium.util;

import java.io.ByteArrayOutputStream;
import java.io.DataOutputStream;
import java.io.IOException;

/**
 * @author coral
 * @version 创建时间：2015年4月22日 类说明
 */
public class Base16 {
	private static final String Base16 = "0123456789ABCDEF";

	private Base16() {
	}

	/**
	 * Convert binary data to a hex-encoded String
	 * 
	 * @param b
	 *            An array containing binary data
	 * @return A String containing the encoded data
	 */
	public static String toString(byte[] b) {
		ByteArrayOutputStream os = new ByteArrayOutputStream();

		for (int i = 0; i < b.length; i++) {
			short value = (short) (b[i] & 0xFF);
			byte high = (byte) (value >> 4);
			byte low = (byte) (value & 0xF);
			os.write(Base16.charAt(high));
			os.write(Base16.charAt(low));
		}
		return new String(os.toByteArray());
	}

	/**
	 * Convert a hex-encoded String to binary data
	 * 
	 * @param str
	 *            A String containing the encoded data
	 * @return An array containing the binary data, or null if the string is
	 *         invalid
	 */
	public static byte[] fromString(String str) {
		ByteArrayOutputStream bs = new ByteArrayOutputStream();
		byte[] raw = str.getBytes();
		for (int i = 0; i < raw.length; i++) {
			if (!Character.isWhitespace((char) raw[i]))
				bs.write(raw[i]);
		}
		byte[] in = bs.toByteArray();
		if (in.length % 2 != 0) {
			return null;
		}

		bs.reset();
		DataOutputStream ds = new DataOutputStream(bs);

		for (int i = 0; i < in.length; i += 2) {
			byte high = (byte) Base16.indexOf(Character.toUpperCase((char) in[i]));
			byte low = (byte) Base16.indexOf(Character.toUpperCase((char) in[i + 1]));
			try {
				ds.writeByte((high << 4) + low);
			} catch (IOException e) {
			}
		}
		return bs.toByteArray();
	}
}
